package com.acme.service.bank;

import com.acme.domain.account.Account;
import com.acme.domain.account.AccountType;
import com.acme.domain.account.CheckingAccount;
import com.acme.domain.bank.Bank;
import com.acme.domain.bank.Client;

import java.math.BigDecimal;

public class BankStatistics {
    private final Bank bank;

    public BankStatistics(Bank bank) {
        this.bank = bank;
    }

    public int getClientsCount(Bank bank) {
        return bank.getClients().size();
    }

    public int getAccountsCount(Bank bank) {
        int accountsCount = 0;
        for (Client client : bank.getClients()) {
            accountsCount += client.getAccounts().size();
        }
        return accountsCount;
    }

    public BigDecimal getTotalBankCredit(Bank bank) {
        BigDecimal credit = new BigDecimal(0);
        for (Client client : bank.getClients()) {
            for (Account account : client.getAccounts()) {
                if (account.getAccountType() == AccountType.CHECKING) {
                    credit = credit.add(((CheckingAccount) account).getCredit());
                }
            }
        }
        return credit;
    }

    public String print() {
        StringBuilder builder = new StringBuilder();
        String newline = System.getProperty("line.separator");
        builder.append("Total number of clients: %s").append(getClientsCount(bank)).append(newline);
        builder.append("Total number of accounts: %s").append(getAccountsCount(bank)).append(newline);
        builder.append("Total bank credit: %s").append(getTotalBankCredit(bank)).append(newline);
        builder.append(bank);
        return builder.toString();
    }
}